// pages/goods/detail.js
var app = getApp();
var a = app.requirejs("core");
var WxParse = app.requirejs("wxParse/wxParse");
Page({
  data: {
    scrollTop:0,
    num:1,
    show:false,
    display: false,
    isOn: false,
    n_animationData: '',
    news_num: 0, //未读消息
    get_phone: false,
    show_rec: 0, // 是否显示海报推荐人
    show_chat: 0, // 是否显示聊天
  },

  onGotUserInfo: function (q) {
    var that = this,
        cache = app.getCache("userinfo");

    // if (cache) {
    //   return;
    // }
    app.getUserInfo(q.detail.userInfo, function (t) {
      console.log("t",t);
      if (t != 1000) {
        var ucache = getApp().getCache("userinfo");
        if (ucache) {
          //getCurrentPages()[getCurrentPages().length - 1].onLoad();
          that.setData({
            display: false,
            getPhoneAuth: getApp().globalData.getphone
          })
        }
      } else {
        that.setData({
          display: true
        })
      }

    }, q.detail.encryptedData, q.detail.iv);

  },

  // 授权时获取用户手机号
  getMobile: function (e) {
    app.globalData.getphone = false;
    this.setData({
      getPhoneAuth: false
    })
    app.getPhoneNumber(e, this, 3);
  },

  // 授权获取用户信息判断
  authCheck: function () {
    var cache = getApp().getCache("userinfo");
    if (!cache.nickName && !cache.avatarUrl) {
      this.setData({
        display: true
      })
      return false;
    } else {
      this.setData({
        display: false
      })
      return true;
    }
  },

  // 取消登录
  cancel_auth: function () {
    this.setData({
      display: false
    })
  },

  to_chat: function() {
    a.jump('/yb_mingpian/pages/chats/chats');
  },

  // 聊天时获取用户手机号
  getPhoneNumber: function(e) {
    app.getPhoneNumber(e,this,1);
  },

  onLoad: function (options) {
    var self = this;
    var cache = app.getCache("userinfo");
    if (!cache.nickName && !cache.avatarUrl) {
      getApp().authSwitch(function(res){
        if (res == 1) {
          self.setData({
            display: true
          })
        } else {
          self.setData({
            display: false
          })
        }
      })
    }

    var gid = this.gid = options.gid ? options.gid : options.id;

    if (options.scene != undefined) {
      var scene = decodeURIComponent(options.scene);
      scene = scene.split(',');
      gid = scene[0];
      options.card_id = scene[2];

      if (options.card_id != undefined) {
        app.globalData.card_id = options.card_id;
      }
    } else {
      if (options.card_id != undefined) {
        app.globalData.card_id = options.card_id;
      }
    }
    console.log("options",options)

    this.setData({
      gid: gid
    })
    console.log(gid);

    app.get_news_num(this);
  },

  onShow:function(){
    var self = this;
    var systemInfo = wx.getSystemInfoSync();

    app.userlog(6, '拼团商品', 'group_browse', self.data.gid);
    this.hideModal();

    this.setData({
      num : 1
    });

    a.get('Pintuan/ptGoodsDetail',{
      gid: self.data.gid,
      uid: app.getCache("userinfo").uid != undefined ? app.getCache("userinfo").uid : 0
    },function(t){
      console.log("ptinfo",t)
      if(t.code==0){
        if(t.info.intro){
          WxParse.wxParse("wxParseData", "html", t.info.intro, self, "0");
        }
        self.setData({
          windowHeight: systemInfo.windowHeight,
          goodsDetail: t.info,
          show_chat: t.info.show_chat
        })
        var groupList = t.info.groupList;
        if (groupList.length > 0) {
          for (var i = 0; i < groupList.length; i++) {
            var t = --groupList[i].leftTime;
            var h = Math.floor(t / 60 / 60);
            var m = Math.floor((t - h * 60 * 60) / 60);
            var s = t % 60;
            if (h < 10) h = "0" + h;
            if (m < 10) m = "0" + m;
            if (s < 10) s = "0" + s;
            groupList[i].leftTimeStr = h + ':' + m + ':' + s
          }
          self.setTimeData(groupList);
        }
        self.setData({
          groupList, groupList,
          show:true
        })
      }else{
        a.alert(t.msg);
      }
    },true)
  },

  // 我的拼团
  toMyGroup: function() {
    a.jump('/yb_mingpian/pages/pintuan/pages/group/index');
  },

  // 分享
  onShareAppMessage: function (res) {
    this.hideModal();
    app.userlog(2, '拼团商品', 'group_relay');

    return {
      title: getApp().getCache('userinfo').nickName + "邀请您拼团，团购价" + this.data.goodsDetail.gprice+"元",  //this.goodsDetail.name,
      path: "/yb_mingpian/pages/pintuan/pages/goods/detail?gid=" + this.data.goodsDetail.id + "&card_id=" + app.globalData.card_id,
      success:function(res){
        console.log("res")
      } 
    }
  },

  setTimeData:function(data){
    var self = this;
    var groupList = data;
    setInterval(function(){
      for (var i = 0; i < groupList.length; i++) {
        var t = --groupList[i].leftTime;
        var h =  Math.floor(t/60/60);
        var m = Math.floor((t-h*60*60)/60);
        var s = t%60;
        if(h<10) h = "0"+h;
        if(m<10) m = "0"+m;
        if(s<10) s = "0"+s;
        groupList[i].leftTimeStr = h+':'+m+':'+s
      }
      self.setData({
        groupList:groupList
      })
    }, 1000)
  },
  joinGroup:function(e){
    var id = e.currentTarget.dataset.id;
    app.redirect('group/detail','id='+id);
  },

  // 跳到名片详情页
  goHome:function(){
    a.jump('/yb_mingpian/pages/cardinfo/index', 4);
  },

  goToBuy:function(){
    var auth_res = this.authCheck();
    if (!auth_res) {
      return;
    }

    var goodsDetail = this.data.goodsDetail;
    goodsDetail['num'] = this.data.num;
    goodsDetail['goodsPrice'] = this.data.goodsPrice;
    goodsDetail['buyType'] = this.data.buyType;
    goodsDetail['groupPid'] = 0;
    app.goodsInfo = goodsDetail; 
  
    app.redirect('goods/payfor');
  },
  selectProp:function(e){
    var current = e.currentTarget.dataset;
    var pid = current.pid;
    var pname = current.pname;
    var name = current.name;
    var propValue = this.propValue ? this.propValue : [];
    propValue[pid] = {pname:pname,name:name};
    this.propValue = propValue;
    this.setData({
      propValue:propValue
    });
  },
  minus:function(){
    var num = this.data.num > 1 ? --this.data.num : this.data.goodsDetail.stock > 0 ? 1 : 0
    this.setData({
      num : num
    });
  },
  plus:function(){
    if (this.data.goodsDetail.stock <= 0) {
      a.alert("库存不足");
      return false;
    }
    if (this.data.buyType == 1 || this.data.goodsPrice == 0) {
      a.alert("限购数量为1");
      return false;
    }

    var num = this.data.num < this.data.goodsDetail.stock ? ++this.data.num : this.data.goodsDetail.stock;
    this.setData({
      num : num
    });
  },
  showModal:function(e){
    var type = e.currentTarget.dataset.type;
    var showModalStatus = e.currentTarget.dataset.statu== 'open' ? true : false;
    var goodsPrice = type == 'group' ? this.data.goodsDetail.gprice : this.data.goodsDetail.price;
    var buyType = type=='group'? 1 : 0;
    app.showModal(this);
    this.setData({
      showModalStatus:showModalStatus,
      goodsPrice:goodsPrice,
      buyType:buyType,
      num : this.data.goodsDetail.stock > 0 ? 1 : 0
    });
  },
  scrolltolower:function(){

  },
  showServer:function(e){
    // var showServer = e.currentTarget.dataset.statu== 'open' ? true : false;
    // app.showModal(this)
    // this.setData({
      // showServer:showServer
    // })
  },

  //底部弹窗设置
  show_Modal: function () {
    var that = this;

    a.get("poster/getConf", {
          type: 3
        }, function (res) {
          if (res.code == 0) {
            that.setData({
              show_rec: res.info.is_set
            })
          }

          var cache = getApp().getCache('userinfo');
          if (!cache.nickName && !cache.avatarUrl) {
            that.setData({
              show_rec: false
            })
          }

          // 显示遮罩层
          var animation = wx.createAnimation({
            duration: 200,
            timingFunction: "ease-in-out",
            delay: 0
          })
          that.animation = animation
          animation.translateY(500).step()
          that.setData({
            animationData: animation.export(),
            isOn: true
          })
          setTimeout(function () {
            animation.translateY(0).step()
            that.setData({
              animationData: animation.export()
            })
          }.bind(that), 200)
    })

  },

  hideModal: function () {
    this.setData({
      isOn: false,
    })
  }
});